package com.qdy.service.impl;

import com.qdy.entity.exercise.Exercise;
import com.qdy.entity.exercise.ExerciseComment;
import com.qdy.entity.exercise.ExerciseMember;
import com.qdy.repository.ExerciseCommentRepository;
import com.qdy.repository.ExerciseMemberRepository;
import com.qdy.repository.ExerciseRepository;
import com.qdy.service.ExerciseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by qudingyi on 2017/4/22.
 */
@Service
public class ExerciseServiceImpl implements ExerciseService {

    @PersistenceUnit
    private EntityManagerFactory emf;
    @Autowired
    private ExerciseRepository exerciseRepository;
    @Autowired
    private ExerciseMemberRepository exerciseMemberRepository;
    @Autowired
    private ExerciseCommentRepository exerciseCommentRepository;

    @Override
    public Map<String,Object> getExerciseInfo(Long exerciseId) {
        Map<String,Object> maps=new HashMap<>();
        maps.put("exercise",exerciseRepository.getOne(exerciseId));
        maps.put("members",exerciseMemberRepository.findByExerciseIdAndStateOrderByCreateTimeDesc(exerciseId,1));
        return maps;
    }

    @Override
    public void saveExercise(Exercise exercise) {
         exerciseRepository.save(exercise);
    }

    @Override
    public void saveComment(ExerciseComment exerciseComment) {
        exerciseCommentRepository.save(exerciseComment);
    }

    @Override
    public void saveMember(ExerciseMember exerciseMember) {
        exerciseMemberRepository.save(exerciseMember);
    }

    @Override
    public List<Exercise> findExerciseByAll() {
        return exerciseRepository.findAll();
    }
}
